{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ecb77c58-fdee-4be7-8acd-4ef28b6be09a",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 27.湿度传感器检测实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：27.湿度传感器检测实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：湿度传感器检测实验\n",
    "湿度传感器检测实验，通过adafruit_dht库可以直接读取对应的温度值和湿度值，然后在jupyter 界面下打印出温度值和湿度值！！！！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18eeae8d-41bf-4a01-9bfa-9905f97d7aca",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6783bd1e-41be-4c92-b09b-c1db11e9fdec",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import board\n",
    "import adafruit_dht"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "993b83e8-b271-4420-9619-a216088a8f56",
   "metadata": {},
   "source": [
    "## 2.DHT11 温湿度传感器管脚定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e495a5d8-b906-402d-b6d2-198f0c76555c",
   "metadata": {},
   "outputs": [],
   "source": [
    "makerobo_dhtDevice = adafruit_dht.DHT11(board.D17) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81ed90-ea51-4447-b522-85494d089b54",
   "metadata": {},
   "source": [
    "## 3.功能函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "20561d89-e334-41e0-9358-07f9b385ffdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def loop():\n",
    "    while True:\n",
    "        try:\n",
    "            # 将数值打印到串口\n",
    "            temperature_c = makerobo_dhtDevice.temperature\n",
    "            temperature_f = temperature_c * (9 / 5) + 32\n",
    "            humidity = makerobo_dhtDevice.humidity\n",
    "            print(\"Temp: {:.1f} F / {:.1f} C    Humidity: {}% \".format(temperature_f, temperature_c, humidity))\n",
    "        except RuntimeError as error:\n",
    "            print(error.args[0])\n",
    "            time.sleep(2.0)\n",
    "            continue\n",
    "        except Exception as error:\n",
    "            dhtDevice.exit()\n",
    "            raise error\n",
    "        time.sleep(2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b01200-b8f4-4906-9fef-ee5921375822",
   "metadata": {},
   "source": [
    "## 4.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45e18609-d482-4008-9b7f-64ee3599f522",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temp: 69.8 F / 21.0 C    Humidity: 67% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Checksum did not validate. Try again.\n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n",
      "Temp: 66.2 F / 19.0 C    Humidity: 44% \n"
     ]
    }
   ],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "    try:\n",
    "        while True:\n",
    "            loop()\n",
    "    except KeyboardInterrupt:\n",
    "        pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
